<?php

/**
 * tema actions.
 *
 * @package    preuvideo
 * @subpackage tema
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class temaActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->temas = Doctrine_Core::getTable('tema')
                        ->createQuery('a')
                        ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->tema = Doctrine_Core::getTable('tema')->find(array($request->getParameter('id')));

        $idAnterior = $this->tema->getIdTemaAnterior();
        $idSiguiente = $this->tema->getIdTemaSiguiente();
        $this->videoNuevo = $request->getParameter('video_nuevo');

        if (!$this->videoNuevo) {
            $this->videoNuevo = 'nada';
        }


        $this->temaAnterior = Doctrine_Core::getTable('tema')->findOneById($idAnterior);
        $this->temaSiguiente = Doctrine_Core::getTable('tema')->findOneById($idSiguiente);
        $this->forward404Unless($this->tema);
    }

    public function executeNew(sfWebRequest $request) {
        $this->form = new temaForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new temaForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($tema = Doctrine_Core::getTable('tema')->find(array($request->getParameter('id'))), sprintf('Object tema does not exist (%s).', $request->getParameter('id')));
        $this->form = new temaForm($tema);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($tema = Doctrine_Core::getTable('tema')->find(array($request->getParameter('id'))), sprintf('Object tema does not exist (%s).', $request->getParameter('id')));
        $this->form = new temaForm($tema);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($tema = Doctrine_Core::getTable('tema')->find(array($request->getParameter('id'))), sprintf('Object tema does not exist (%s).', $request->getParameter('id')));
        $tema->delete();

        $this->redirect('tema/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $tema = $form->save();

            $this->redirect('tema/edit?id=' . $tema->getId());
        }
    }

    function executeCargarComentarios(sfWebRequest $request) {
        $id = $request->getParameter('id');

        $tema = Doctrine_Core::getTable('tema')->findOneById($id);

        $comentarios = $tema->getComentarios();
        $max = FacebookPropio::sacarIdMaximo('comentario');

        $user = FacebookPropio::check_connection();

        if ($request->isXmlHttpRequest()) {
            return $this->renderPartial('comentarios', array('tema' => $tema, 'user' => $user, 'comentarios' => $comentarios, 'max' => $max));
        }
    }

    function executeRefrescarMax(sfWebRequest $request) {
        if ($request->isXmlHttpRequest()) {
            return $this->renderPartial('max');
        }
    }

    function executeAgregarComentarioRaiz(sfWebRequest $request) {
        $tema_id = $request->getParameter('tema_id');
        $facebook_id = urldecode($request->getParameter('facebook_id'));
        $texto = urldecode($request->getParameter('texto_pregunta'));

        $usuarios[] = $facebook_id;
        $datosFacebook = FacebookPropio::facebookQuery($usuarios);

        $comentario = new Comentario();

        $comentario->setTemaId($tema_id);
        $comentario->setFacebookId($facebook_id);
        $comentario->setTexto($texto);
        $comentario->setIdComentarioAnterior(0);

        $comentario->save();

        if ($request->isXmlHttpRequest()) {
            return $this->renderPartial('comentario', array('comentario' => $comentario, 'datosFacebook' => $datosFacebook));
        }
    }

    function executeAgregarComentarioNodo(sfWebRequest $request) {
        $comentario_id = $request->getParameter('comentario_id');
        $comentario_raiz = Doctrine_Core::getTable('comentario')->findOneById($comentario_id);
        $tema_id = $request->getParameter('tema_id');
        $facebook_id = urldecode($request->getParameter('facebook_id'));
        $texto = urldecode($request->getParameter('texto_respuesta' . $comentario_id));

        $usuarios[] = $facebook_id;
        $datosFacebook = FacebookPropio::facebookQuery($usuarios);

        $comentario = new Comentario();

        $comentario->setTemaId($tema_id);
        $comentario->setFacebookId($facebook_id);
        $comentario->setTexto($texto);
        $comentario->setIdComentarioAnterior($comentario_raiz->getMayorNodo());
        $comentario->save();

        if ($request->isXmlHttpRequest()) {
            return $this->renderPartial('comentario_nodo', array('comentario' => $comentario, 'datosFacebook' => $datosFacebook));
        }
    }

    public function executeSelectTema(sfWebRequest $request) {
        $this->forward404Unless($id = $request->getParameter("tema_id"));
        $temas = Doctrine_Core::getTable('Tema')->findById($id);
        //echo count($ejes);
        return $this->renderPartial('selectTema', array('temas' => $temas));
    }

    public function executeActualizarDetalleVideo(sfWebRequest $request) {
        $video_id = $request->getParameter('video_id');
        $el_video = Doctrine_Core::getTable('video')->findOneById($video_id);

        if ($request->isXmlHttpRequest()) {
            return $this->renderPartial('detalleVideo', array('el_video' => $el_video));
        }
    }

}

